package com.hmkj.admin.controller.risk;

import com.hmkj.admin.controller.BaseController;
import com.hmkj.admin.controller.GenericController;
import com.hmkj.common.entity.ResultEntity;
import com.hmkj.common.utils.StringUtils;
import com.hmkj.core.exception.BussinessException;
import com.hmkj.core.po.commo.Commo;
import com.hmkj.core.po.risk.RiskParam;
import com.hmkj.core.service.commo.CommoService;
import com.hmkj.core.service.risk.RiskParamService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(BaseController.BASE_URI)
public class RiskJsonController extends GenericController {

    @Resource
    private RiskParamService riskParamService;
    @Resource
    private CommoService commoService;

    /**
     * 交易品种列表json数据
     */
    @ResponseBody
    @RequestMapping(value = "risk/json/detail", method = RequestMethod.POST)
    public ResultEntity listJson(Integer id) throws Exception {
        if(null == id){
            throw new BussinessException("参数有误");
        }
        Commo commo = commoService.selectByPrimaryKey(id);
        if(null == commo){
            throw new BussinessException("交易品种不存在");
        }
        List<RiskParam> riskList = riskParamService.select(new RiskParam(r ->{
            r.setCommoId(id);
        }));
        return ok(riskList);
    }

    /**
     * 交易品种修改
     */
    @ResponseBody
    @RequestMapping(value = "risk/json/edit", method = RequestMethod.POST)
    @RequiresPermissions("sys:risk:edit")
    public ResultEntity edit(Integer id,Integer type,String value) throws Exception {
        if(null == id){
            throw new BussinessException("交易品种参数有误");
        }
        if(StringUtils.isBlank(value)){
            throw new BussinessException("交易品种参数值参数有误");
        }

        RiskParam risk = riskParamService.selectByPrimaryKey(id);
        if(null == risk ){
            throw new BussinessException("风控参数不存在 ");
        }
        Commo commo = commoService.selectByPrimaryKey(risk.getCommoId());
        if(null == commo){
            throw new BussinessException("交易品种不存在");
        }
        riskParamService.updateRisk(commo,risk,type,value);
        return ok();

    }
}